if (SERVER) then

	AddCSLuaFile( "shared.lua" )


end

if ( CLIENT ) then

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 70
	SWEP.ViewModelFlip		= true
	SWEP.CSMuzzleFlashes	= false
	SWEP.BounceWeaponIcon	= false
	// This is the font that's used to draw the death icons
	surface.CreateFont( "csd", ScreenScale( 30 ), 500, true, true, "CSKillIcons" )
	surface.CreateFont( "csd", ScreenScale( 60 ), 500, true, true, "CSSelectIcons" )
	
	SWEP.PrintName			= "weapon_hegrenade"			
	SWEP.Author				= "Counter-Strike"

	SWEP.Slot				= 3
	SWEP.SlotPos			= 1
	SWEP.IconLetter			= "h"
	
	killicon.AddFont( "weapon_hegrenade", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )
	killicon.AddFont( "ent_explosivegrenade", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )
end

SWEP.Weight				= 5
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false
SWEP.HoldType			= "grenade"

SWEP.Author			= "Counter-Strike"
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= ""
SWEP.Category		= "Counter-Strike DS"


SWEP.Spawnable 				= true
SWEP.AdminSpawnable 		= true

SWEP.ViewModel 				= "models/weapons/v_eq_fraggrenade.mdl"
SWEP.WorldModel 			= "models/weapons/w_eq_fraggrenade.mdl"

SWEP.Primary.ClipSize 			= -1
SWEP.Primary.DefaultClip 		= 1
SWEP.Primary.Automatic 			= false
SWEP.Primary.Ammo 				= "grenade"
SWEP.Primary.Delay				= 1

SWEP.Secondary.ClipSize 		= -1
SWEP.Secondary.DefaultClip 		= -1
SWEP.Secondary.Automatic 		= false
SWEP.Secondary.Ammo 			= "none"

function SWEP:Initialize()
		self:SetWeaponHoldType( self.HoldType )
end

function SWEP:Deploy()

	self.Weapon:SetNextPrimaryFire( CurTime() + 1 )

	self.Weapon:SendWeaponAnim( ACT_VM_DRAW )

	return true
end

function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )
	draw.SimpleText( self.IconLetter, "CSSelectIcons", x + wide/2, y + tall*0.2, Color( 255, 210, 0, 255 ), TEXT_ALIGN_CENTER )
end

function SWEP:Reload()

end

function SWEP:Throw()
	if (!SERVER) then return end
	if self.Owner:GetActiveWeapon() != self.Weapon then return end

	local tr = self.Owner:GetEyeTrace()

	local ent = ents.Create ("ent_explosivegrenade")

	local v = self.Owner:GetShootPos() + self.Owner:GetForward() * 2 + self.Owner:GetRight() * 3 - self.Owner:GetUp() * 3
	ent:SetPos( v )
	ent.Owner = self.Owner
	ent:SetAngles ( Angle( math.random( 1, 100 ), math.random( 1, 100 ), math.random( 1, 100 ) ) )
	ent:Spawn()

	local phys = ent:GetPhysicsObject()
	if !phys or !phys:IsValid() then return end

	self.Weapon:SendWeaponAnim( ACT_VM_THROW )
	self.Owner:SetAnimation( PLAYER_ATTACK1 )

	phys:SetVelocity( self.Owner:GetAimVector() * 1000 + Vector( 0, 0, 100) + self.Owner:GetVelocity() )

	phys:AddAngleVelocity( Vector( math.random( -500, 500 ), math.random( -500, 500 ), math.random( -500, 500 ) ) )

	self.Owner:RemoveAmmo(1, self.Primary.Ammo)

	timer.Simple( 0.6, function()
		if self.Owner:GetAmmoCount( self.Primary.Ammo ) > 0 then
			if self.Owner:GetActiveWeapon() != self.Weapon then return end
			self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
		else
			self.Weapon:Remove()
			self.Owner:ConCommand("lastinv")
		end
	end)
end

function SWEP:PrimaryAttack()
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:SendWeaponAnim(ACT_VM_PULLPIN)
	timer.Simple( 1, function()
		if self:IsValid() then
			self:Throw()
		else
			if CLIENT then
				LocalPlayer():ConCommand("lastinv")
			end
		end
	end)
end

function SWEP:SecondaryAttack()
	return false
end